using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using UnityEditor;

public static class BuildApkClientTool
{
    private static string BuildFolder = "Build/APK/";
    private static string BuildPath = GEditorTool.ProjectPath + BuildFolder;

    [MenuItem("[FC Release]/BuildSetting/重置客户端构建版本号", false, 0)]
    private static void ResetClientVersion()
    {
        PlayerSettings.bundleVersion = "1.0.0";
        PlayerSettings.Android.bundleVersionCode = 0;
    }

    [MenuItem("[FC Release]/APK/生成内网测试包", false, 1)]
    private static void BuildLocalDebugApk()
    {
        Hashtable appInfo = new Hashtable();
        appInfo["ChannelType"] = BuildClientTool.ChannelType.LocalDebug;
        appInfo["AppName"] = "tafang_localdebug" + "_" + DateTime.Now.ToString("yyyyMMddHHmm") + ".apk";
        appInfo["ChannelName"] = "内网测试";
        appInfo["IsRelease"] = false;
        appInfo["AppVersion"] = "1.0.0";
        appInfo["IsRDG"] = false;
        BuildApkClient(appInfo);
        DebugUtil.Log("生成内网测试包成功");
    }

    [MenuItem("[FC Release]/APK/生成外网审核包", false, 2)]
    private static void BuildNetCheckApk()
    {
        Hashtable appInfo = new Hashtable();
        appInfo["ChannelType"] = BuildClientTool.ChannelType.NetCheck;
        appInfo["AppName"] = "tafang_netszmonster" + "_" + DateTime.Now.ToString("yyyyMMddHHmm") + ".apk";
        appInfo["ChannelName"] = "外网审核";
        appInfo["IsRelease"] = true;
        appInfo["AppVersion"] = "1.0.0";
        appInfo["IsRDG"] = true;
        BuildApkClient(appInfo);
        DebugUtil.Log("生成外网审核包成功");
    }

    [MenuItem("[FC Release]/APK/生成外网正式包", false, 3)]
    private static void BuildNetReleaseApk()
    {
        Hashtable appInfo = new Hashtable();
        appInfo["ChannelType"] = BuildClientTool.ChannelType.NetRelease;
        appInfo["AppName"] = "tafang_netrelease" + "_" + DateTime.Now.ToString("yyyyMMddHHmm") + ".apk";
        appInfo["ChannelName"] = "外网正式";
        appInfo["IsRelease"] = true;
        appInfo["AppVersion"] = "1.0.0";
        appInfo["IsRDG"] = false;
        BuildApkClient(appInfo);
        DebugUtil.Log("生成外网正式包成功");
    }

    private static void BuildApkClient(Hashtable appInfo)
    {
        if (!Directory.Exists(BuildFolder))
            Directory.CreateDirectory(BuildFolder);
        if (!Directory.Exists(BuildClientTool.AppInfoFolder))
            Directory.CreateDirectory(BuildClientTool.AppInfoFolder);

        string buildPath = BuildFolder + appInfo["AppName"];

        bool isRelease = (bool)appInfo["IsRelease"];
        bool isRemoteDll = (bool)appInfo["IsRDG"];
        SetClientVersion(appInfo, isRelease);
        SetAndroidKeystore();

        BuildClientTool.CreateAppInfo(appInfo);
        BuildClientTool.SetScriptingDefineSymbols(BuildTargetGroup.Android, isRemoteDll);
        BuildClientTool.BuildPlayer(buildPath, BuildTarget.Android);
        BuildClientTool.ClearScriptingDefineSymbols(BuildTargetGroup.Android, isRemoteDll);

        Process.Start(BuildPath);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }

    private static void SetClientVersion(Hashtable appInfo, bool isRelease)
    {
        PlayerSettings.bundleVersion = (string)appInfo["AppVersion"];
        if (isRelease)
        {
            int bundleVersionCode = PlayerSettings.Android.bundleVersionCode;
            PlayerSettings.Android.bundleVersionCode = bundleVersionCode + 1;
        }
    }

    private static void SetAndroidKeystore()
    {
        PlayerSettings.Android.keystoreName = GEditorTool.ProjectPath + "Keystore/tfgame.keystore";
        PlayerSettings.Android.keystorePass = "szmtfgame";
        PlayerSettings.Android.keyaliasName = "game";
        PlayerSettings.Android.keyaliasPass = "emagftmzs";
    }
}